package com.example.springbootxscjxt.controller;

import com.example.springbootxscjxt.entity.Score;
import com.example.springbootxscjxt.mapper.ScoreMapper;
import com.example.springbootxscjxt.mapper.StudentMapper;
import com.example.springbootxscjxt.service.ScoreService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private ScoreService scoreService;

    @GetMapping("/index")
    public Map<String,Object> index(@RequestParam String studentName, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        pageNum=(pageNum-1)*pageSize;
        Map<String ,Object> res=new HashMap<>();
        List<Score> data=scoreMapper.select(studentName,pageNum,pageSize);
        Integer total=scoreMapper.total(studentName);
        res.put("code",200);
        res.put("data",data);
        res.put("total",total);
        return res;
    }
    @GetMapping("/all")
    public Map<String,Object> all(){
        Map<String ,Object> res=new HashMap<>();
        List<Score> data=scoreMapper.selectall();
        res.put("code",200);
        res.put("data",data);
        return res;


    }
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody Score score){
       Map<String,Object> res=new HashMap<>();
       if(scoreService.add(score) == 1){
           res.put("code",200);
           res.put("msg","新增成功");

       }else{
           res.put("code",500);
           res.put("msg","新增失败");

       }
        return res;
    }
    @PostMapping("/update")

    public Map<String,Object> update(@RequestBody Score score){
        Map<String,Object> res=new HashMap<>();
        if(scoreService.update(score) == 1){
            res.put("code",200);
            res.put("msg","修改成功");

        }else{
            res.put("code",500);
            res.put("msg","修改失败");

        }
        return  res;

    };
    @GetMapping("delete")
    public  Map<String,Object> shanchu(@RequestParam String id){
        Map<String,Object> res=new HashMap<>();
        if(scoreMapper.scscore(id) == 1){
            res.put("code",200);
            res.put("msg","删除成功");

        }else{
            res.put("code",500);
            res.put("msg","删除失败");

        }
        return res;
    }
@GetMapping("/my")
    public  Map<String,Object> myscore(@RequestParam String studentNo){
        Map<String,Object> res=new HashMap<>();
        List<Score> data=scoreMapper.cxgrcj(studentNo);
        res.put("data",data);
        res.put("code",200);
        return res;


}

}
